package com.zykj.synchronizevolatile;

/**
 * @author zhoulj(周利军) [1217102780@qq.com]
 * @Module ID:   <(模块)类编号，可以引用系统设计中的类编号>
 * @Comments: <对此类的描述，可以引用系统设计中的描述>
 * @JDK version used:      <JDK1.8> 28
 * @since 2019/5/22-15:28
 */
public class SynchronizedDemo {

    private boolean ready = false;
    private int result = 1 ;
    private int number =1 ;

    public void write() {
        ready = true;
        number =2 ;
    }
    public void read() {
        if (ready) {
            result = number * 3;
        }
        System.out.println("result:=" + result);
    }

    private class WriteOrReadyThread extends Thread {
        private boolean flag ;
        public WriteOrReadyThread(boolean flag) {
            this.flag = flag;
        }

        @Override
        public void run() {
            if (flag) {

                write();
            } else {
                read();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        SynchronizedDemo demo = new SynchronizedDemo();


        demo.new WriteOrReadyThread(true).start();
        Thread.sleep(100);
        demo.new WriteOrReadyThread(false).start();
    }
}
